import pygame
import random
import math

# 初始化Pygame
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()

# 天空渐变参数
SKY_TOP = (135, 206, 235)    # 顶部天蓝
SKY_BOTTOM = (224, 255, 255) # 底部浅蓝

# ========== 太阳类 ==========
class Sun:
    def __init__(self):
        self.x = WIDTH - 100
        self.y = 100
        self.radius = 50      # 核心半径
        self.ray_count = 12    # 光线数量
        self.ray_length = 70  # 光线长度
        self.angle = 0        # 浮动角度
        
    def update(self):
        self.angle += 0.02  # 浮动速度
        
    def draw(self, surface):
        # 绘制光晕效果
        for i in range(3):
            glow_radius = self.radius + i*15
            alpha = 80 - i*20
            glow_surf = pygame.Surface((glow_radius*2, glow_radius*2), pygame.SRCALPHA)
            pygame.draw.circle(glow_surf, (255, 255, 180, alpha), 
                             (glow_radius, glow_radius), glow_radius)
            surface.blit(glow_surf, (self.x - glow_radius, self.y - glow_radius))
        
        # 绘制核心
        pygame.draw.circle(surface, (255, 215, 0), (self.x, self.y), self.radius)
        
        # 绘制动态光线
        angle_step = 360 / self.ray_count
        current_angle = self.angle * 30  # 光线旋转速度
        for _ in range(self.ray_count):
            rad_angle = math.radians(current_angle)
            start_x = self.x + self.radius * math.cos(rad_angle)
            start_y = self.y + self.radius * math.sin(rad_angle)
            end_x = start_x + self.ray_length * math.cos(rad_angle)
            end_y = start_y + self.ray_length * math.sin(rad_angle)
            
            # 光线渐变效果
            for i in range(3):
                line_width = 5 - i*2
                alpha = 200 - i*50
                pygame.draw.line(surface, (255, 165 + i*30, 0, alpha),
                               (start_x, start_y), (end_x, end_y), line_width)
            current_angle += angle_step

# ========== 飞鸟类 ==========
class Bird:
    def __init__(self):
        self.x = random.randint(-200, WIDTH+200)
        self.y = random.randint(50, 300)
        self.speed = random.uniform(2, 5)
        self.wing_phase = random.uniform(0, 2*math.pi)  # 翅膀相位
        
    def update(self):
        self.x += self.speed
        self.wing_phase += 0.2  # 翅膀扇动速度
        if self.x > WIDTH + 50:
            self.x = -50
            self.y = random.randint(50, 300)
            
    def draw(self, surface):
        # 翅膀动态参数
        wing_height = 15 * abs(math.sin(self.wing_phase))
        
        # 绘制鸟身
        points = [
            (self.x, self.y),
            (self.x + 30, self.y - wing_height),
            (self.x + 40, self.y),
            (self.x + 30, self.y + wing_height),
            (self.x, self.y)
        ]
        pygame.draw.polygon(surface, (40, 40, 40), points)

# ========== 云朵类 ========== 
class Cloud:
    def __init__(self):
        self.reset()
        self.size = random.randint(30, 60)
        self.puffs = self.generate_puffs()
        
    def reset(self):
        self.x = random.randint(-200, WIDTH)
        self.y = random.randint(50, 300)
        self.speed = random.uniform(0.3, 1.2)
        
    def generate_puffs(self):
        return [(random.randint(-self.size, self.size), 
                random.randint(-self.size//2, self.size//2), 
                random.randint(self.size-10, self.size+10)) 
                for _ in range(5)]
    
    def update(self):
        self.x += self.speed
        if self.x > WIDTH + 200:
            self.reset()
            
    def draw(self, surface):
        for dx, dy, r in self.puffs:
            alpha = 220 - (r/self.size)*40
            cloud_surf = pygame.Surface((r*2, r*2), pygame.SRCALPHA)
            pygame.draw.circle(cloud_surf, (255,255,255,alpha), 
                             (r, r), r)
            surface.blit(cloud_surf, (self.x + dx, self.y + dy))

# ========== 主程序 ==========
sun = Sun()
birds = [Bird() for _ in range(4)]
clouds = [Cloud() for _ in range(6)]

running = True
while running:
    # 绘制渐变天空
    for y in range(HEIGHT):
        factor = y / HEIGHT
        color = (
            SKY_TOP[0] * (1 - factor) + SKY_BOTTOM[0] * factor,
            SKY_TOP[1] * (1 - factor) + SKY_BOTTOM[1] * factor,
            SKY_TOP[2] * (1 - factor) + SKY_BOTTOM[2] * factor
        )
        pygame.draw.line(screen, color, (0, y), (WIDTH, y))
    
    # 事件处理
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            
    # 更新元素状态
    sun.update()
    [bird.update() for bird in birds]
    [cloud.update() for cloud in clouds]
    
    # 绘制所有元素
    sun.draw(screen)
    [bird.draw(screen) for bird in birds]
    [cloud.draw(screen) for cloud in clouds]
    
    pygame.display.update()
    clock.tick(30)

pygame.quit()